/*-+-*****************************************************************************
 Copyright (C) 2009-2012 Oceanside Software Corporation (R)
 Dallas, Texas

 Author       : Jason T. Brower
 Purpose      : This file is part of a point-of-sale software package
                originally written for Table-Service Restaurants.
              
 Date         : July 8th of 2012
              
 History      : The application that this software file belongs to was 
                written over a 3-Year period.  The first version was written
                in C# .Net 3.5 using Windows Forms.  Being new to .Net at
                the time, I soon learned of Windows Presentation Foundation,
                liked the graphics capabilities and databinding so I
                migrated to WPF.  When the .Net 4.0 framework was released
                the application was migrated from 3.5 to 4.0. Now today,
                .Net 4.5 RC is out and I was already in process of migrating
                before I made this public.
                
Excuse Me     : Please excuse the coding style, it is not representative of
                my typical work.  During the three years I was under a very
                tight budget, constantly running out of funding and was 
                working 18 hour days hyped up on 5-Hour Energy.  The 
                design is not textbook.  It is monolythic because of
                previous issues I will not get into.  There are few comments
                in the code and the ones that are there may be incorrect.
                
What it is not: This is not the exact version that I took through PA-DSS 2.0
                nor is it the exact version running at my customer sites.
                I removed some details for security purposes including some
                encryption algorithms.  The reporting was removed because
                I was including a third party library that cannot be 
                published with this Open Source.
                
What it is    : This application has been running at high volume restaraunts
                for over a year.  It never freezes, crashes or does anything
                unexpected.  So long as it is built, installed and configured
                correctly, it is a strong system that we can all enhance.

Contact       : Please note that I can no longer work for free.  However, you
                can reach me at information@oceansidesw.com.                                                      

*********************************************************************************/
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Media;

namespace Oceanside
{
    public static class Functions
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="rightNow"></param>
        /// <param name="startTime"></param>
        /// <param name="endTime"></param>
        /// <returns></returns>
        public static bool IsInBetweenTimeFrame(DateTime rightNow, DateTime startTime, DateTime endTime)
        {
            if (startTime != null && endTime != null)
            {
                ///The first case is for 24/7
                if (startTime.TimeOfDay == endTime.TimeOfDay)
                {
                    return true;
                }
                ///The simple case such as 1:00 PM to 7:00PM
                if (endTime.TimeOfDay > startTime.TimeOfDay)
                {
                    if (rightNow.TimeOfDay >= startTime.TimeOfDay && rightNow.TimeOfDay <= endTime.TimeOfDay)
                    {
                        return true;
                    }
                }
                ///The case such as 10:00 PM to 3:00 AM and the current time is >= 10:00PM yet not rolled over to 00:00:00 24-hour clock.
                else if (rightNow.TimeOfDay >= startTime.TimeOfDay && rightNow.Hour > 12)
                {
                    return true;
                }
                ///This is the case such as 10:00 PM to 3:00 AM and the current time has rolled into the next day
                else
                {
                    TimeSpan span = endTime.TimeOfDay - startTime.TimeOfDay;
                    DateTime adjustedEnd =
                        new DateTime(
                            rightNow.Year,
                            rightNow.Month,
                            rightNow.Day,
                            startTime.Hour,
                            startTime.Minute,
                            startTime.Second);

                    adjustedEnd = adjustedEnd.Add(span);

                    if (rightNow < adjustedEnd)
                    {
                        return true;
                    }
                }
            }
            return false;
        }

        public static T TryFindParent<T>(DependencyObject current) where T : class
        {
            DependencyObject parent = VisualTreeHelper.GetParent(current);
            if (parent == null)
                return null;
            if (parent is T)
                return parent as T;
            else return TryFindParent<T>(parent);
        }

        public static void ListViewCustTicket_MouseDoubleClick(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            GridViewColumnHeader header = Functions.TryFindParent<GridViewColumnHeader>(e.OriginalSource as DependencyObject);
            if (header != null)
            {
                switch (header.Content.ToString())
                {
                    case "QTY":
                        header.Column.Width = 40;
                        break;
                    case "INS":
                        header.Column.Width = 40;
                        break;
                    case "PRODUCT":
                        header.Column.Width = 160;
                        break;
                    case "PRICE EA":
                        header.Column.Width = 81;
                        break;
                }
            }
        }

        public static void Thumb_DragDelta(object sender, DragDeltaEventArgs e)
        {
            try
            {
                Thumb senderAsThumb = e.OriginalSource as Thumb;

                if (senderAsThumb != null)
                {
                    GridViewColumnHeader header = senderAsThumb.TemplatedParent as GridViewColumnHeader;
                    if (header != null && header.Content != null)
                    {
                        switch (header.Content.ToString())
                        {

                            case "QTY":
                                header.Column.Width = 40;
                                break;
                            case "INS":
                                header.Column.Width = 40;
                                break;
                            case "PRODUCT":
                                header.Column.Width = 160;
                                break;
                            case "PRICE EA":
                                header.Column.Width = 81;
                                break;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Logging.LogException(ex);
            }
        }
    }
}
